﻿Partial Public Class Window

    ''' <summary>
    ''' Adds text to the end of the window's content.
    ''' </summary>
    ''' <param name="value">The text to add.</param>
    ''' <param name="onNewLine">True to add this text on a new line, otherwise false.</param>
    Public Sub AppendText(value As String, Optional onNewLine As Boolean = False)
        If onNewLine AndAlso Content.TextLength > 0 Then value = ControlChars.NewLine & value
        Content.Text &= value
    End Sub

    Public Sub ClearText()
        Content.Text = String.Empty
    End Sub

    ''' <summary>
    ''' Brings the window forward in the z-order.
    ''' </summary>
    Public Overridable Sub BringForward()
        Dim domethod As New Action(Of Window)(Sub(w)
                                                  Dim index = openWindows.IndexOf(Me)
                                                  openWindows.RemoveAt(index)
                                                  openWindows.Insert(index + 1, Me)
                                              End Sub)
        domethod(Me)
        Dim p As Window = Me.Parent
        While p IsNot Nothing
            If Not p.CanFocus Then domethod(p)
            p = p.Parent
        End While
    End Sub

    ''' <summary>
    ''' Brings the window to the front of the z-order, placing it on top of all other windows.
    ''' </summary>
    Public Overridable Sub BringToFront()
        Dim domethod As New Action(Of Window)(Sub(w)
                                                  Dim index = openWindows.IndexOf(w)
                                                  openWindows.RemoveAt(index)
                                                  openWindows.Add(w)
                                              End Sub)
        domethod(Me)
        Dim p As Window = Me.Parent
        While p IsNot Nothing
            If Not p.CanFocus Then domethod(p)
            p = p.Parent
        End While
    End Sub

    ''' <summary>
    ''' Stops displaying the window, making it invisible and sending it to the back of the z-order.
    ''' </summary>
    Public Overridable Sub Hide()
        _Visible = False
        SendToBack()
        OnHidden(EventArgs.Empty)
    End Sub

    ''' <summary>
    ''' Brings the next window in the z-order after this window to the front of the z-order.
    ''' </summary>
    Public Overridable Sub NextWindow()
        Dim index As Integer = openWindows.IndexOf(Me)
        Dim startIndex As Integer = index
        Do
            index += 1
            If index = openWindows.Count Then index = 0
            Dim w = openWindows(index)
            If w.CanFocus AndAlso w.Visible Then Exit Do
            If index = startIndex Then Exit Do
        Loop
        openWindows(index).BringToFront()
    End Sub

    ''' <summary>
    ''' Scrolls the content of the window down.
    ''' </summary>
    Public Overridable Sub ScrollDown()
        If Not atBottom Then firstLineVisible += 1
    End Sub

    ''' <summary>
    ''' Scrolls the content of the window up.
    ''' </summary>
    Public Overridable Sub ScrollUp()
        If firstLineVisible > 0 Then firstLineVisible -= 1

    End Sub

    ''' <summary>
    ''' Sends the window backward in the z-order.
    ''' </summary>
    Public Overridable Sub SendBackward()
        Dim domethod As New Action(Of Window)(Sub(w)
                                                  Dim index = openWindows.IndexOf(w)
                                                  If index > 0 Then
                                                      openWindows.RemoveAt(index)
                                                      openWindows.Insert(index - 1, w)
                                                  End If
                                              End Sub)
        domethod(Me)
        Dim p As Window = Me.Parent
        While p IsNot Nothing
            If Not p.CanFocus Then domethod(p)
            p = p.Parent
        End While
    End Sub

    ''' <summary>
    ''' Sends the window to the back of the z-order.
    ''' </summary>
    Public Overridable Sub SendToBack()
        Dim domethod As New Action(Of Window)(Sub(w)
                                                  Dim index = openWindows.IndexOf(w)
                                                  openWindows.RemoveAt(index)
                                                  openWindows.Insert(0, w)
                                              End Sub)
        domethod(Me)
        Dim p As Window = Me.Parent
        While p IsNot Nothing
            If Not p.CanFocus Then domethod(p)
            p = p.Parent
        End While
    End Sub

    ''' <summary>
    ''' Starts displaying the window, making it visible and bringing it to the front of the z-order.
    ''' </summary>
    Public Overridable Sub Show()
        _Visible = True
        BringToFront()
        OnShown(EventArgs.Empty)
    End Sub

End Class
